package com.sy.api;

/**
 * @className: UsersController
 * @author: Mr.Liu
 * @date: 2025/10/29 14:37
 * @Version: 1.0
 * @description:
 */

import com.sy.PageVO;
import com.sy.ResultVO;
import com.sy.dto.UsersPageDTO;
import com.sy.dto.UsersRegisterDTO;
import com.sy.pojo.Users;
import com.sy.service.UsersService;
import com.sy.utils.BCryptUtils;
import com.sy.utils.StringRandom;
import com.sy.vo.UsersPageVO;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.time.LocalDateTime;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/api/users")
@Tag(name = "用户管理模块")
public class UsersController {
    @Resource
    private UsersService usersService;
    //创建日志对象
    Logger log= LoggerFactory.getLogger(UsersController.class);
    @PostMapping("/register")
    @Operation(summary = "用户注册")
    public ResultVO usersRegister(@RequestBody UsersRegisterDTO usersRegisterDTO){
        log.info("注册的请求参数为:{}",usersRegisterDTO);
        Users users = new Users();
        //将属性复制
        BeanUtils.copyProperties(usersRegisterDTO,users);
        //给密码加密
        String verifyPassword = BCryptUtils.hashPassword(users.getPassword());
        //重新赋值给原来的密码
        users.setPassword(verifyPassword);
        //TODO 判断账号是否重复
        //账户的状态/创建的时间/用户名
        users.setStatus(1);
        users.setCreateTime(LocalDateTime.now());
        users.setUpdateTime(LocalDateTime.now());
        //真实用户名
        String name = StringRandom.randomName();
        users.setName(name);
        boolean flag = usersService.save(users);
        if(flag){
            //成功
           return ResultVO.success();
        }else{
            //失败
            return ResultVO.error("注册失败");
        }
    }
    @GetMapping("/page")
    @Operation(summary = "用户分页查询")
    public ResultVO<PageVO<UsersPageVO>> usersPage(@Parameter(description = "分页接收的参数")
                                          UsersPageDTO usersPageDTO){
        log.info("分页参数是:{}",usersPageDTO);
        PageVO<UsersPageVO> usersPageVOPageVO = usersService.queryPage(usersPageDTO);
        return ResultVO.success(usersPageVOPageVO);
    }
    @GetMapping("/update/status")
    @Operation(summary = "状态修改")
    public ResultVO<Object> updateStatus(@Parameter(description = "用户id",required = true) Integer id,
                                         @Parameter(description = "用户的状态",required = true) Integer status){
        log.info("用户的id和状态是:{},{}",id,status);
        boolean flag;
        if(status!=null){
            flag= usersService.updateStatus(id, status);
            if(flag){
                //成功
                return ResultVO.success();
            }else{
                //失败
                return ResultVO.error("修改状态失败");
            }
        }
        return  ResultVO.error("修改状态失败");
    }
    @PostMapping("/login")
    @Operation(summary = "用户登录")
    public ResultVO<Object> userLogin(@RequestBody Map<String,String> map){
      //假设页面传递过来的两个参数的值的分别是phone code
        log.info("验证码登录认证的入参:{}",map);
        String phone = map.get("phone");
        String code = map.get("code");
        if(!StringUtils.hasText(phone)){
            return ResultVO.error("请输入正确的手机号");
        }
        if(!StringUtils.hasText(code)){
            return ResultVO.error("请输入正确的验证码");
        }
        String token = usersService.userLogin(phone, code);
        if(token!=null){
            //将用户信息也查询以下
            Users users = usersService.queryUsersByPhone(phone);
            Map<String,Object> hsmap = new HashMap<>();
            hsmap.put("userInfo",users);
            hsmap.put("token",token);
            return ResultVO.success(hsmap);
        }
        return ResultVO.error("认证失败");
    }
}
